package com.laosg.study.base.ch4;

/**
 * Created by kaimin on 8/5/2019.
 * time : 08:40
 * java动态绑定，
 * 将一个子类的对象 赋值给父类的引用，
 * 如果调用的是方法，jvm会将该方法和对象的内存地址绑定
 * 如果调用的是属性，则没有动态绑定，在哪里调用，就在哪里返回对应值
 */
public class JavaDaynamicBind {
    public static void main(String[] args) {

        A a = new A();
        System.out.println(a.sum());//20
        System.out.println(a.sum1());//20

        A b = new B();
        System.out.println(b.sum());//40
        System.out.println(b.sum1());//30
    }
}

class A{
    private int i=10;
    public int getI(){
        return i;
    }
    public int sum(){
        return getI()+10;
    }
    public int sum1(){
        return i+10;
    }
}

class B extends A{
    private int i=20;

   /* @Override
    public int sum() {
        return i+20;
    }

    @Override
    public int sum1() {
        return i+10;
    }*/

    @Override
    public int getI() {
        return i;
    }
}
